package com.cobra.framework.register;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cobra.framework.URL;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class RemoteMapRegister {

    private static Map<String, List<URL>> REGISTER = new HashMap<>();

    private static Lock LOCK = new ReentrantLock();

    private static String PATH = "data/serverList.txt";
    //视情况而定，这个是为了在每次启动时清除上一次执行的配置文件信息
    public static void clear(URL url,String interfaceName){
        try {
            File file = new File(PATH);
            String json = FileUtils.readFileToString(file);
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONArray array = jsonObject.getJSONArray(interfaceName);
            List<URL> urlList = array.toJavaList(URL.class);
            for (URL obj : urlList) {
                if(obj.toString().equals(url.toString())){
                    FileUtils.writeStringToFile(file,"","UTF-8");
                }
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void regist(String interfaceName, URL url){
        try {
            LOCK.lock();
            clear(url,interfaceName);
            List<URL> list = REGISTER.get(interfaceName);
            if (list == null) {
                list = new ArrayList<>();

            }
            list.add(url);
            REGISTER.put(interfaceName, list);
            saveFile();
        } finally {
            LOCK.unlock();
        }
    }

    public static List<URL> get(String interfaceName) {
        List<URL>  list = null;
        try {
            File file = new File(PATH);
            String json = FileUtils.readFileToString(file);
            JSONObject jsonObject = JSONObject.parseObject(json);
            JSONArray array = jsonObject.getJSONArray(interfaceName);
            list = array.toJavaList(URL.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    private static void saveFile() {
        try {
            File file = new File(PATH);
            Map<String, List<URL>> map = getFile();
            if (map!=null) {
                map.forEach(
                       (key, value) -> {
                           List<URL> urlList = REGISTER.get(key);
                           if(urlList!=null){
                               urlList.addAll(value);
                               REGISTER.put(key,urlList);
                           }
                       }
                );
            }
            FileUtils.writeStringToFile(file,JSONObject.toJSONString(REGISTER),"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Map<String, List<URL>> getFile() {
        try {
            File file = new File(PATH);
            String json = FileUtils.readFileToString(file);
            if(json == null || "".equals(json))
                return null;
            return JSONObject.parseObject(json, REGISTER.getClass());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
